//
//  wgmeViewController.m
//  gpos_app
//
//  Created by apple on 2018/8/22.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "wgmeViewController.h"
#import "Masonry.h"
#import "wgmeViewCell.h"
#import "homeModel.h"
#import "GPosAPI.h"
#import "WKProgressHUD.h"
#import "SVProgressHUD.h"
#import "XMRegisterViewController.h"
@interface wgmeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView * tableView;
@property (nonatomic,strong)WKProgressHUD * hud;
@property (nonatomic,strong)NSMutableArray * dataArr;
@end

@implementation wgmeViewController


//改变状态栏
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.backgroundColor=[UIColor whiteColor];
    self.navigationController.navigationBar.tintColor=[UIColor blackColor];
    self.view.backgroundColor = [UIColor colorWithRed:244/255.0f green:244/255.0f blue:244/255.0f alpha:1];
    self.title = @"券友";
    [self loadData];
    
}
- (void)UIlodaView{
    self.tableView = [[UITableView alloc] init];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(10);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
        make.bottom.equalTo(self.view).with.offset(0);
        
    }];
    
    UIView * headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, KmainScreenWidth, 35);
    headerView.backgroundColor = [UIColor lightGrayColor];
    self.tableView.tableHeaderView = headerView;
    UIView * footerView = [[UIView alloc] init];
    self.tableView.tableFooterView = footerView;
    
    UIView * leftTopView = [[UIView alloc] init];
    leftTopView.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:leftTopView];
    [leftTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headerView).with.offset(0);
        make.top.equalTo(headerView).with.offset(0);
        make.bottom.equalTo(headerView).with.offset(-0.5);
        make.width.equalTo(@80);
    }];
    
    UILabel * leftTopLabel = [[UILabel alloc] init];
    leftTopLabel.text = @"序号";
    leftTopLabel.textAlignment=NSTextAlignmentCenter;
    leftTopLabel.textColor = [UIColor blackColor];
    leftTopLabel.font = [UIFont systemFontOfSize:14];
    [leftTopView addSubview:leftTopLabel];
    [leftTopLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftTopView).with.offset(5);
        make.right.equalTo(leftTopView).with.offset(0);
        make.centerY.equalTo(leftTopView);
    }];
    
    UIView * rightTopView = [[UIView alloc] init];
    rightTopView.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:rightTopView];
    [rightTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftTopView.mas_right).with.offset(0);
        make.right.equalTo(headerView).with.offset(0);
        make.top.equalTo(headerView).with.offset(0);
        make.bottom.equalTo(headerView).with.offset(-0.5);
        
    }];
    
    UILabel * oneLabel = [[UILabel alloc] init];
    oneLabel.text = @"电话";
    oneLabel.textAlignment=NSTextAlignmentCenter;
    oneLabel.textColor = [UIColor blackColor];
    oneLabel.font = [UIFont systemFontOfSize:14];
    [rightTopView addSubview:oneLabel];
    
    UILabel * twoLabel = [[UILabel alloc] init];
    twoLabel.text = @"购买台数";
    twoLabel.textAlignment=NSTextAlignmentCenter;
    twoLabel.textColor = [UIColor blackColor];
    twoLabel.font = [UIFont systemFontOfSize:14];
    [rightTopView addSubview:twoLabel];
    
    [@[oneLabel, twoLabel] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:5 leadSpacing:5 tailSpacing:5];
    
    [@[oneLabel, twoLabel] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(rightTopView);
        
    }];
    
}
- (void)loadData{
    self.dataArr = [[NSMutableArray alloc] init];
    __weak typeof(self) weakSelf = self;
    int user = [[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] intValue];
    NSDictionary * params = @{@"userId" : @(user)};
    [GPosAPI requestDirectInfoWithParams:params andCallback:^(id obj) {
        NSLog(@"ooo---%@",obj);
        if (obj == nil) {
            
            weakSelf.hud=[WKProgressHUD popMessage:[obj objectForKey:@"message"] inView:self.view duration:1 animated:YES];
            
        }else if ([[obj objectForKey:@"code"] integerValue]==200){
            NSArray * arr = [[NSArray alloc] init];
//            self.dataArr = [obj objectForKey:@"data"];
            arr = [obj objectForKey:@"data"];
        
            for (NSDictionary*dic in arr) {

                homeModel *model  = [[homeModel alloc] init];
                model.typeShuju = [dic objectForKey:@"number"];
                model.typePhone = [dic objectForKey:@"phone"];
                [weakSelf.dataArr addObject:model];
            }
            
            [weakSelf.tableView reloadData];
            NSLog(@"1data=%@",self.dataArr);
            
        }else if ([[obj objectForKey:@"code"] integerValue]==401 || [[obj objectForKey:@"code"] intValue] == 402){
            UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
            //storyboard里的ID
            XMRegisterViewController *vc = [mainStory instantiateInitialViewController];
            vc.navigationController.navigationBarHidden=NO;
            //                    //设置当前窗口的根控制器
            
            [UIApplication sharedApplication].keyWindow.rootViewController=vc;
            [SVProgressHUD showInfoWithStatus:[obj objectForKey:@"message"]];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
            [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];

            
        }else {
            
            weakSelf.hud=[WKProgressHUD popMessage:[obj objectForKey:@"message"] inView:self.view duration:1 animated:YES];

            
        }
    }];
    [self UIlodaView];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArr.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//rankingCell

    
    wgmeViewCell * cell = [[wgmeViewCell alloc] init];
    cell = [tableView dequeueReusableCellWithIdentifier:@"wgmeCell"];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"wgmeViewCell" owner:self options:nil] firstObject];
    }
    
    cell.xuhaoLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
    cell.nameLabel.text = [self.dataArr[indexPath.row] typePhone];
    cell.phoneLabel.text = [NSString stringWithFormat:@"%@台",[self.dataArr[indexPath.row] typeShuju]];
    cell.callPhoneButton.tag = indexPath.row;
    [cell.callPhoneButton addTarget:self action:@selector(callPhoneButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 40;
}

- (void)callPhoneButtonAction:(UIButton *)sender{
    homeModel * model = self.dataArr[sender.tag];
    NSLog(@"diahua---%@",model.typePhone);
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",model.typePhone];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
    
    
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed=YES;
    }
    return self;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
